//
// Created by user on 2022/3/15.
//

#include <iostream>
#include <unordered_set>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
    };

//快慢指针
bool hasCycle(ListNode *head) {
    if(head== nullptr || head->next== nullptr)return false;
    ListNode *slow = head;
    ListNode *fast = head;
    while (fast!= nullptr && fast->next!=nullptr){
        slow = slow->next;
        fast = fast->next->next;
        if(slow==fast){
            return true;
        }
    }
    return false;
}

//哈希查找
bool hasCycle1(ListNode *head) {
    unordered_set<ListNode*> set;
    while (head!= nullptr){
        if(set.count(head)){
            return true;
        }
        set.insert(head);
        head = head->next;
    }
    return false;

}